package GUI.User;

import GUI.System.GUIFrame;
import javax.swing.JOptionPane;

/**
 * Account panel for the logged in customer.
 */
public class AccountPanel extends javax.swing.JPanel {

    private GUIFrame frame;

    /**
     * Custom constructor.
     */
    public AccountPanel() {
        initComponents();
    }

    /**
     * Setter for GUIFrame reference.
     *
     * @param frame GUIFrame object.
     */
    public void setFrame(GUIFrame frame) {
        this.frame = frame;
    }

    /**
     * Load customer details into labels.
     */
    public void loadPage() {
        labCName.setText(frame.cManager.getLoggedInUser().getFirstName() + " "
                + frame.cManager.getLoggedInUser().getLastName());
        labCEmail.setText(frame.cManager.getLoggedInUser().getEmail());
        labCCardNumber.setText(frame.cManager.getLoggedInUser().getCardNumb());
        labCBalance.setText(
                frame.cManager.getLoggedInUser().getBalance() * 0.01 + "");
        if (frame.cManager.getLoggedInUser().getAccountStatus().
                equals("PENAPP")) {
            labCAccStatus.setText("Pending Approval");
        } else if (frame.cManager.getLoggedInUser().getAccountStatus().equals(
                "PENACT")) {
            labCAccStatus.setText("Pending Activation");
        } else if (frame.cManager.getLoggedInUser().getAccountStatus().equals(
                "DISABL")) {
            labCAccStatus.setText("Disabled");
        } else if (frame.cManager.getLoggedInUser().getAccountStatus().equals(
                "ACTIVE")) {
            labCAccStatus.setText("Activated");
        }

        if (frame.cManager.getLoggedInUser().getUseStatus().equals("CHAR")) {
            labCUseStatus.setText("Charging");
        } else if (frame.cManager.getLoggedInUser().getUseStatus().
                equals("IDLE")) {
            labCUseStatus.setText("Idle");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labTitle = new javax.swing.JLabel();
        btnEdit = new javax.swing.JButton();
        btnChangePass = new javax.swing.JButton();
        btnAddFunds = new javax.swing.JButton();
        btnTransactionHistory = new javax.swing.JButton();
        labName = new javax.swing.JLabel();
        labCName = new javax.swing.JLabel();
        labEmail = new javax.swing.JLabel();
        labCEmail = new javax.swing.JLabel();
        labCardNumber = new javax.swing.JLabel();
        labCCardNumber = new javax.swing.JLabel();
        labBalance = new javax.swing.JLabel();
        labCBalance = new javax.swing.JLabel();
        btnLogout = new javax.swing.JButton();
        labCUseStatus = new javax.swing.JLabel();
        labUseStatus = new javax.swing.JLabel();
        labCAccStatus = new javax.swing.JLabel();
        labAccStatus = new javax.swing.JLabel();
        btnDeleteAccount = new javax.swing.JButton();
        btnTerminalList = new javax.swing.JButton();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        labTitle.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        labTitle.setText("Account Information");
        add(labTitle, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1, -1));

        btnEdit.setText("Edit account details");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        add(btnEdit, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 80, 140, -1));

        btnChangePass.setText("Change password");
        btnChangePass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangePassActionPerformed(evt);
            }
        });
        add(btnChangePass, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, 140, -1));

        btnAddFunds.setText("Add funds");
        btnAddFunds.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddFundsActionPerformed(evt);
            }
        });
        add(btnAddFunds, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, 140, -1));

        btnTransactionHistory.setText("Transaction history");
        btnTransactionHistory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTransactionHistoryActionPerformed(evt);
            }
        });
        add(btnTransactionHistory, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 200, 140, -1));

        labName.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labName.setText("Name");
        add(labName, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 80, -1, -1));

        labCName.setText(" ");
        add(labCName, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 100, 181, -1));

        labEmail.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labEmail.setText("Email");
        add(labEmail, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 120, -1, -1));

        labCEmail.setText(" ");
        add(labCEmail, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 140, 170, -1));

        labCardNumber.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labCardNumber.setText("Card Number");
        add(labCardNumber, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 160, -1, -1));

        labCCardNumber.setText(" ");
        add(labCCardNumber, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 180, 60, -1));

        labBalance.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labBalance.setText("Balance (DKK)");
        add(labBalance, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 200, -1, -1));

        labCBalance.setText(" ");
        add(labCBalance, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 220, 60, -1));

        btnLogout.setText("Logout");
        btnLogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLogoutActionPerformed(evt);
            }
        });
        add(btnLogout, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 332, -1, -1));

        labCUseStatus.setText(" ");
        add(labCUseStatus, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 300, 102, -1));

        labUseStatus.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labUseStatus.setText("Use status");
        add(labUseStatus, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 280, 93, -1));

        labCAccStatus.setText(" ");
        add(labCAccStatus, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 260, 125, -1));

        labAccStatus.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labAccStatus.setText("Account status");
        add(labAccStatus, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 240, 102, -1));

        btnDeleteAccount.setText("Delete account");
        btnDeleteAccount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteAccountActionPerformed(evt);
            }
        });
        add(btnDeleteAccount, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 283, 140, -1));

        btnTerminalList.setText("View all terminals");
        btnTerminalList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTerminalListActionPerformed(evt);
            }
        });
        add(btnTerminalList, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 240, 140, -1));
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Change to login panel.
     *
     * @param evt ActionEvent
     */
    private void btnLogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLogoutActionPerformed
        frame.changePanel("card1");
        frame.cManager.setLoggedInUser(null);
    }//GEN-LAST:event_btnLogoutActionPerformed

    /**
     * Change to panel for editing account details.
     *
     * @param evt ActionEvent
     */
    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        frame.changePanel("card8");
    }//GEN-LAST:event_btnEditActionPerformed

    /**
     * Change to panel for changing password.
     *
     * @param evt ActionEvent
     */
    private void btnChangePassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangePassActionPerformed
        frame.changePanel("card9");
    }//GEN-LAST:event_btnChangePassActionPerformed

    /**
     * Change to transaction history panel.
     *
     * @param evt ActionEvent
     */
    private void btnTransactionHistoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTransactionHistoryActionPerformed
        frame.changePanel("card10");
    }//GEN-LAST:event_btnTransactionHistoryActionPerformed

    /**
     * Change to panel for adding funds.
     *
     * @param evt ActionEvent
     */
    private void btnAddFundsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddFundsActionPerformed
        frame.changePanel("card15");
    }//GEN-LAST:event_btnAddFundsActionPerformed

    /**
     * Checks fist that customer does not have any more funds available. If so,
     * asks the customer to accept wishing to delete his account. If confirmed,
     * the customer is disabled in the database. The change to login panel.
     *
     * @param evt ActionEvent
     */
    private void btnDeleteAccountActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteAccountActionPerformed
        if (frame.cManager.getLoggedInUser().getBalance() != 0) {
            JOptionPane.showMessageDialog(this,
                    "Sorry, you can not delete your account\n"
                    + "as you still have some funds available.\n"
                    + "You will either have to use the remaining\n"
                    + "funds, or you can contact customers support.");
        } else {
            int reply = JOptionPane.showConfirmDialog(null,
                    "Are you sure you want to delete your account?\n"
                    + "If you choose to delete your account, you will\n"
                    + "be logged out and you will not be able to login\n"
                    + "or reenable your account unless you contact\n"
                    + "customers support.\n "
                    + "Are you sure you want to proceed deleting your "
                    + "account\n", "Are you sure?", JOptionPane.YES_NO_OPTION);
            if (reply == JOptionPane.YES_OPTION) {
                frame.changePanel("card1");
                String[] newValues = new String[1];
                newValues[0] = "DISABL";
                frame.cManager.updateCustomerInformation(frame.cManager.
                        getLoggedInUser().getCustomerNumb(), 7, newValues);
                frame.cManager.setLoggedInUser(null);
            }
        }
    }//GEN-LAST:event_btnDeleteAccountActionPerformed

    /**
     * Change to terminal-list panel.
     *
     * @param evt ActionEvent.
     */
    private void btnTerminalListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTerminalListActionPerformed
        frame.changePanel("card20");
    }//GEN-LAST:event_btnTerminalListActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddFunds;
    private javax.swing.JButton btnChangePass;
    private javax.swing.JButton btnDeleteAccount;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnLogout;
    private javax.swing.JButton btnTerminalList;
    private javax.swing.JButton btnTransactionHistory;
    private javax.swing.JLabel labAccStatus;
    private javax.swing.JLabel labBalance;
    private javax.swing.JLabel labCAccStatus;
    private javax.swing.JLabel labCBalance;
    private javax.swing.JLabel labCCardNumber;
    private javax.swing.JLabel labCEmail;
    private javax.swing.JLabel labCName;
    private javax.swing.JLabel labCUseStatus;
    private javax.swing.JLabel labCardNumber;
    private javax.swing.JLabel labEmail;
    private javax.swing.JLabel labName;
    private javax.swing.JLabel labTitle;
    private javax.swing.JLabel labUseStatus;
    // End of variables declaration//GEN-END:variables
}
